package com.zhuxulin.tab;

import java.io.IOException;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import com.zhuxulin.tab.listeners.GestureListener;
import com.zhuxulin.tab.utils.LogUtils;

@SuppressWarnings("deprecation")
public class Main extends TabActivity {
	protected static final String TAG = Main.class.getSimpleName();
	private static final String Tab1 = "Tab1";
	private static final String Tab2 = "Tab2";
	private static final String Tab3 = "Tab3";
	private static final String Tab4 = "Tab4";
	private static final String Tab5 = "Tab5";
	private TabHost tabHost = null;
	private GestureDetector gestureDetector = null;

	// private TabSpec spec1 = null;
	// private TabSpec spec2 = null;
	// private TabSpec spec3 = null;
	// private TabSpec spec4 = null;
	// private TabSpec spec5 = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		tabHost = getTabHost();
		initTabHost();

		gestureDetector = new GestureDetector(this,
				new GestureListener(tabHost));
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		LogUtils.i(TAG, "Code:" + keyCode + "Action:" + event.getAction()
				+ "Repeat:" + event.getRepeatCount());
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void initTabHost() {
		LayoutInflater.from(this).inflate(R.layout.activity_main,
				tabHost.getTabContentView(), true);
		tabHost.addTab(tabHost.newTabSpec(Tab1).setIndicator("Music")
				.setContent(new Intent(this, Music.class)));
		tabHost.addTab(tabHost.newTabSpec(Tab2).setIndicator("Piano")
				.setContent(new Intent(this, Piano.class)));
		tabHost.addTab(tabHost.newTabSpec(Tab3).setIndicator("Timer")
				.setContent(new Intent(this, MidTimer.class)));
		tabHost.addTab(tabHost.newTabSpec(Tab4).setIndicator("Setting")
				.setContent(new Intent(this, Setting.class)));
		tabHost.addTab(tabHost.newTabSpec(Tab5).setIndicator("PianoTimer")
				.setContent(new Intent(this, PianoUseTimer.class)));

		tabHost.setCurrentTab(0);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		App app = (App) getApplication();
		if (app.getSocket() == null || !app.getSocket().isConnected()) {
			return;
		}
		try {
			LogUtils.i(TAG, "Activity onDestroy begin close socket!");
			app.getSocket().close();
			// android.os.Process.killProcess(android.os.Process.myPid());
			LogUtils.i(TAG, "Activity onDestroy close socket successful!");
		} catch (IOException e) {
			LogUtils.i(TAG, "Activity onDestroy close socket throws exception!");
			e.printStackTrace();
		}
	}

	// @Override
	// public boolean dispatchTouchEvent(MotionEvent event) {
	//
	// if (gestureDetector.onTouchEvent(event)) {
	// event.setAction(MotionEvent.ACTION_CANCEL);
	// }
	// return super.dispatchTouchEvent(event);
	// }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
